//
//  GTNewsViewController.m
//  news
//
//  Copyright © 2022 sinosun. All rights reserved.
//

#import "GTNewsViewController.h"
#import "GTNormalTableViewCell.h"
#import "GTDetailViewController.h"
#import "GTDeleteCellView.h"
#import "GTListLoader.h"
#import "GTListItem.h"

#pragma mark TestView 声明UIView 的子类 TestView
@interface TestView : UIView
@end
// 实现这个子类，并重载其中的UIView的生命周期的方法
@implementation TestView

- (instancetype)init{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (void)willMoveToSuperview:(nullable UIView *)newSuperview{
    [super willMoveToSuperview:newSuperview];
}
- (void)didMoveToSuperview{
    [super didMoveToSuperview];
    
}
- (void)willMoveToWindow:(nullable UIWindow *)newWindow{
    [super willMoveToWindow:newWindow];
}
- (void)didMoveToWindow{
    [super didMoveToWindow];
    
}

@end



@interface GTNewsViewController () <UITableViewDataSource, UITableViewDelegate, GTNormalTableViewCellDelegate>

@property (nonatomic, strong, readwrite) UITableView *tableView;
@property (nonatomic, strong, readwrite) NSArray<GTListItem *> *dataArray;
@property (nonatomic, strong, readwrite) GTListLoader* loader;
@end

@implementation GTNewsViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        _loader = [[GTListLoader alloc] init];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    if (false) {
        TestView *testView = [[TestView alloc] init];
        testView.backgroundColor = [UIColor greenColor];
        testView.frame = CGRectMake(100, 100, 100, 100);
        
        [self.view addSubview:testView];
        
        UITapGestureRecognizer* gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushController)];
        [testView addGestureRecognizer:gestureRecognizer];
        
        
    }
    
    // UITableView 列表空间
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    [self.view addSubview:_tableView];
    
    //[_loader loadListData];
    __weak typeof(self)wself = self;
    [_loader loadListDataWithBlock:^(BOOL success, NSArray<GTListItem *> *dataArray){
        __strong typeof(wself)sself = wself;
        if (success) {
            sself.dataArray = dataArray;
            [sself.tableView reloadData];
        }
    }];
    
    
}

- (void)pushController{
    UIViewController *vontroller = [[UIViewController alloc] init];
    vontroller.view.backgroundColor = [UIColor whiteColor];
    vontroller.navigationItem.title = @"内容";
    
    vontroller.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"右侧按钮" style:UIBarButtonItemStylePlain target:self action:nil];
    [[self navigationController] pushViewController:vontroller animated:YES];
}

#pragma mark --UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    // return 20; 这里之前写的20，如果UITableView删除一个cell后会导致这里的20与删除后的19不一致，导致崩溃
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    // 先去系统的回收池里面取一个cell
    GTNormalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"id"];
    if (!cell) {
        cell = [[GTNormalTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"id"];
        cell.deleagte = self;
    }
    //[cell layoutTableViewCell];
    [cell layoutTableViewCellWithGTListItem:_dataArray[indexPath.row]];
//    cell.textLabel.text = [NSString stringWithFormat:@"主标题 -- %@", @(indexPath.row)];
//    cell.detailTextLabel.text = @"细节信息";
    return cell;
}

#pragma mark --UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

// 点击一个cell时的响应
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GTDetailViewController *controller = [[GTDetailViewController alloc] initWithUrl:_dataArray[indexPath.row].articUrl];
    controller.title = [NSString stringWithFormat:@"位置 %@",@(indexPath.row)];
    [self.navigationController pushViewController:controller animated:YES];
    
    // 标记一下第几个被已读了
    GTListItem *item = [_dataArray objectAtIndex:indexPath.row];
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:item.uniquekey];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSLog(@"UITableView did scroll %f", scrollView.contentOffset.y);
}

#pragma mark - GTNormalTableViewCellDelegate
- (void)tableViewCell:(UITableViewCell *)tableViewCell clickDeleteButton:(UIButton *)deletaButton{
    GTDeleteCellView *deleteView  = [[GTDeleteCellView alloc] initWithFrame:self.view.bounds];
    //[deleteView showDeleteView];
    
    
    /**
     deletaButton 是在 tableViewCell 中
     而deleteView是直接在uiwindow中
     所以这里需要将 tableViewCell 坐标系 转换到在 uiwindow的坐标系
     */
    CGRect rect = [tableViewCell convertRect:deletaButton.frame toView:nil];
    
    // bolck解决循环引用的问题
    __weak typeof(self) weakSelf = self;
    [deleteView showDeleteViewWithPoint:rect.origin clickBlock:^{
        __strong typeof(weakSelf) strongSelf = weakSelf;
        // [strongSelf.dataArray removeLastObject];
        // 执行删除操作
        [strongSelf.tableView deleteRowsAtIndexPaths:@[[self.tableView indexPathForCell:tableViewCell]] withRowAnimation:(UITableViewRowAnimationAutomatic)];
    }];
    
}
@end
